package cn.pdx.elec.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.pdx.elec.dao.ElecCommonMsgDao;
import cn.pdx.elec.domain.ElecCommonMsg;
import cn.pdx.elec.service.ElecCommonMsgService;
import cn.pdx.elec.web.form.ElecCommonMsgForm;



/**
 *  @Repository 、@Service 和 @Controller 
           在目前的 Spring 版本中，这 3 个注释和 @Component 是等效的，但是从注释类的命名上，3 个注释分别和持久层 、业务层 和控制层 相对应。
 * 
 *@Transactional(readOnly=true),定义类方法的事务为只读,不能写入数据库
 * 业务类方法只有增、删、改为可写,其他事务都为只读
 * @author 彭定旭
 */

@Transactional(readOnly=true)
@Service(ElecCommonMsgService.SERVICE_NAME)
public class ElecCommonMsgServiceImpl implements ElecCommonMsgService {
   
	
	//业务层调用dao层;用注解,把相应的dao注入进去
	@Resource(name=ElecCommonMsgDao.SERVICE_NAME)
	private ElecCommonMsgDao elecCommonMsgDao;
   
	
	/**
	 * @Name:findElecCommonMsgList
	 * @Description:所有代办事宜 列表查询
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-3-27(创建日期)
	 * @parameters:
	 * @return:List<ElecCommonMsgForm>集合对象
	 */
	public List<ElecCommonMsgForm> findElecCommonMsgList() {
		
		/**
		 * hqlWhere为空,params也没有参数传,orderby按日期排序
		 */
		String hqlWhere="";
		Object [] params=null;
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put(" o.createDate", "desc");
		//返回PO对象,不是VO对象
		List<ElecCommonMsg> list=elecCommonMsgDao.findCollectionByConditionNoPage(hqlWhere, params, orderby);
		
		//把PO对象转换成VO对象,封装一下,抽一个方法
		List<ElecCommonMsgForm> formList=this.elecCommonMsgListPOListToVOList(list);
		
		return formList;
	}

  
	/**
	 * @Name:elecCommonMsgListPOListToVOList
	 * @Description:将所有代办事宜的结果信息从PO对象转换为VO对象
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-3-27(创建日期)
	 * @parameters:List<ElecCommonMsg> list PO对象结果集
	 * @return:List<ElecCommonMsgForm>  VO对象结果集
	 */
	private List<ElecCommonMsgForm> elecCommonMsgListPOListToVOList(List<ElecCommonMsg> list) {
		
		List<ElecCommonMsgForm> formList=new ArrayList<ElecCommonMsgForm>();
		
		ElecCommonMsgForm  elecCommonMsgForm=null;
		
		for(int i=0;list!=null && i<list.size();i++)
		{
			ElecCommonMsg elecCommonMsg=list.get(i);
			
			elecCommonMsgForm=new ElecCommonMsgForm();
			
			elecCommonMsgForm.setComID(elecCommonMsg.getComID());
			elecCommonMsgForm.setDevRun(elecCommonMsg.getDevRun());
			elecCommonMsgForm.setStationRun(elecCommonMsg.getStationRun());
			elecCommonMsgForm.setCreateDate(String.valueOf(elecCommonMsg.getCreateDate()!=null?elecCommonMsg.getCreateDate():" "));
			
			formList.add(elecCommonMsgForm);
		}
		return formList;
	}

 
	/**
	 * @Name:saveElecCommonMsg
	 * @Description:保存代办事宜对象
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-3-28(创建日期)
	 * @parameters:无
	 * @return:无
	 * 要设置为可写方法才能写入数据库
	 */
	@Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false)
	public void saveElecCommonMsg(ElecCommonMsgForm elecCommonMsgForm) {
		
		//VO对象转成PO对象
		ElecCommonMsg elecCommonMsg=this.elecCommonMsgLVOToPO(elecCommonMsgForm);
		elecCommonMsgDao.save(elecCommonMsg);
	}

    
	/**
	 * @Name:elecCommonMsgLVOToPO
	 * @Description:页面传递的代办事宜信息从VO对象转换成PO对象
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-3-28(创建日期)
	 * @parameters:ElecCommonMsgForm elecCommonMsgForm  页面对象
	 * @return:无
	 */
	private ElecCommonMsg elecCommonMsgLVOToPO(ElecCommonMsgForm elecCommonMsgForm) {
		
		ElecCommonMsg elecCommonMsg=new ElecCommonMsg();
		if(elecCommonMsgForm!=null)
		{
			elecCommonMsg.setStationRun(elecCommonMsgForm.getStationRun());
			elecCommonMsg.setDevRun(elecCommonMsgForm.getDevRun());
			elecCommonMsg.setCreateDate(new Date());
		}
		return elecCommonMsg;
	}


	/**
	 * @Name:findElecCommonMsgListByCurrentDate
	 * @Description:查询代办事宜当天列表,在ElecLoginAction里面调用这个方法,然后显示在首页,顿
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-3-28(创建日期)
	 * @parameters:
	 * @return:List<ElecCommonMsgForm> 页面ElecCommonMsgForm VO对象
	 * 一般查询某些字段返回的是Object 类型数组,我是这么觉得.............
	 */
	public List<ElecCommonMsgForm> findElecCommonMsgListByCurrentDate() {
		
		//获取当前日期 YYYY-MM-DD
		java.sql.Date date=new java.sql.Date(new java.util.Date().getTime());
		String currentDate=date.toString();
		
		List<Object []> list=elecCommonMsgDao.findElecCommonMsgListByCurrentDate(currentDate);
		
		//将Object数组变成VO对象
		List<ElecCommonMsgForm> formList=this.elecCommonMsgObjectListToVOList(list);
		
		return formList;
	}


	/**
	 * @Name:elecCommonMsgObjectListToVOList
	 * @Description:查询代办事宜由Object数组转换成VO对象
	 * @author 彭定旭
	 * @version：v1.00（版本号）
	 * @Create Date:2013-3-28(创建日期)
	 * @parameters:List<Object[]> list 存放Object[]数组对象
	 * @return:List<ElecCommonMsgForm> 页面ElecCommonMsgForm VO对象
	 * 
	 */
	private List<ElecCommonMsgForm> elecCommonMsgObjectListToVOList(
			List<Object[]> list) {
		
		List<ElecCommonMsgForm> formList=new ArrayList<ElecCommonMsgForm>();
			
		ElecCommonMsgForm elecCommonMsgForm=null;
		
		for(int i=0;list!=null && i<list.size();i++)
		{
			Object []objects=list.get(i);
			elecCommonMsgForm=new ElecCommonMsgForm();
			elecCommonMsgForm.setStationRun(objects[0].toString());
			elecCommonMsgForm.setDevRun(objects[1].toString());
			formList.add(elecCommonMsgForm);
		}
		return formList;
	}


	
	
	
	
	
}
